package com.example.tool.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.tool.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liangkun on 2018/8/20.
 * 作用:圆柱形view
 */

public class BarRoundChartView extends View {
    private Paint mPaint, mChartPaint;
    private Rect mBound;
    private int mHeight, mWidth, mChartWidth, mSize;
    private List<Float> list = new ArrayList<>();
    private getNumberListener listener;
    private int number = 1000;
    private int selectIndex = -1;
    private List<Integer> selectIndexRoles = new ArrayList<>();
    private List<RectF> rectFList = new ArrayList<>();
    private float centerX;
    private float top;
    private int commonBarColor;
    private int selectBarColor;
    private int bottomTextColor;
    private float bottomTextSize;
    private float barTextSpace;


    public void setList(List<Float> list) {
        this.list = list;
        invalidate();
    }

    public BarRoundChartView(Context context) {
        this(context, null);
    }

    public BarRoundChartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BarRoundChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BarRoundChartView, defStyleAttr, 0);
        commonBarColor = typeArray.getColor(R.styleable.BarRoundChartView_commonBarColor, Color.BLACK);
        selectBarColor = typeArray.getColor(R.styleable.BarRoundChartView_selectBarColor, Color.BLACK);
        bottomTextColor = typeArray.getColor(R.styleable.BarRoundChartView_bottomTextColor, Color.BLACK);
        bottomTextSize = typeArray.getDimension(R.styleable.BarRoundChartView_bottomTextSize, 35);
        barTextSpace = typeArray.getDimension(R.styleable.BarRoundChartView_barTextSpace, 50);

        typeArray.recycle();
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width;
        int height;
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = widthSize  / 2;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = heightSize  / 2;
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
//        mWidth = getWidth();
//        mHeight = getHeight();
        mWidth = getMeasuredWidth();
        mHeight =getMeasuredHeight();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mBound = new Rect();
        mChartPaint = new Paint();
        mChartPaint.setAntiAlias(true);
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        rectFList.clear();
        mSize = getWidth() / 35; //控制柱状图的宽度和间距
        mChartWidth = getWidth() / 13 - mSize / 2 - 40; //bar的宽度
        for (int i = 0; i < 12; i++) {
            RectF rectF = null;
            int size = mHeight / 120;
            mChartPaint.setStyle(Paint.Style.FILL);
            if (list.size() > 0) {
                if (selectIndexRoles.contains(i)) {
                    //选中的view
                    mChartPaint.setShader(null);
                    mChartPaint.setColor(selectBarColor);
                } else {
                    //普通的view
                    mChartPaint.setColor(commonBarColor);
                }
                //后面几个月的数据没有
                if (i >= 8) {
                    mChartPaint.setColor(Color.GRAY);
                    //画柱状图
                    rectF = new RectF();
                    rectF.left = mChartWidth;
                    rectF.right = mChartWidth + mSize;
                    rectF.bottom = mHeight -barTextSpace;
                    //最大值是120*5
//                rectF.top = mHeight-120*5 ;
                    rectF.top = rectF.bottom - mSize;
                    canvas.drawRoundRect(rectF, 20, 20, mChartPaint);
                } else {
                    //画柱状图
                    rectF = new RectF();
                    rectF.left = mChartWidth;
                    rectF.right = mChartWidth + mSize;
                    rectF.bottom = mHeight - barTextSpace;
                    //最大值是120*5
//                rectF.top = mHeight-120*5 ;
                    rectF.top = rectF.bottom - list.get(i) * size - 2;
                    canvas.drawRoundRect(rectF, 20, 20, mChartPaint);
                }
                //画数字
                mPaint.setColor(bottomTextColor);
                mPaint.setTextSize(bottomTextSize);
                mPaint.setTextAlign(Paint.Align.CENTER);
                mPaint.getTextBounds(String.valueOf(i + 1), 0, String.valueOf(i).length(), mBound);
                canvas.drawText(String.valueOf(i + 1), rectF.centerX(),
                        mHeight - barTextSpace/2 + mBound.height()/2, mPaint);
                mChartWidth += getWidth() / 13 + 8;

            }
            //非空
            if (rectF != null) {
                rectFList.add(rectF);
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        int x = (int) ev.getX();
        int y = (int) ev.getY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                for (int i = 0; i < 12; i++) {
                    RectF rectF = rectFList.get(i);
                    if (rectF.contains(x, y)) {
                        if (i >= 8) {
                            break;
                        }
                        if (listener != null) {
                            //坐标
                            centerX = rectF.centerX();
                            top = rectF.top;
                            listener.getNumber(i, centerX, top);
                            selectIndex = i;
                            selectIndexRoles.clear();
                            selectIndexRoles.add(selectIndex);
                            postInvalidate();
                        }
                    }

                }
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return true;
    }

    public void setListener(getNumberListener listener) {
        this.listener = listener;
    }

    public interface getNumberListener {
        void getNumber(int number, float x, float y);
    }

}
